package com.zhfi.dataapply.schedule.writer.chaincode;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.chaincode.CompanyMapProvinceCountPO;
import com.zhfi.dataapply.mapper.chaincode.CompanyMapProvinceCountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class CompanyMapProvinceCountWriter implements ItemWriter<CompanyMapProvinceCountPO> {

    @Autowired
    private CompanyMapProvinceCountMapper mapper;

    @Override
    public void write(List<? extends CompanyMapProvinceCountPO> list) throws Exception {
        for (CompanyMapProvinceCountPO po : list) {
            try {
                UpdateWrapper<CompanyMapProvinceCountPO> wrapper = new UpdateWrapper<>();
                wrapper.eq("chain_code", po.getChainCode());
                wrapper.eq("company_ipo_type", po.getCompanyIpoType());
                wrapper.eq("finance_type", po.getFinanceType());
                wrapper.eq("province", po.getProvince());
                int count = mapper.update(po, wrapper);
                if (count <= 0) {
                    mapper.insert(po);
                }
            } catch (Exception e) {
                log.error("CompanyMapProvinceCountWriter写入失败，数据：" + po, e);
            }
        }
    }
}
